package spatialmarketplace.cem.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.util.Log;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.ValidationError;
import spatialmarketplace.cem.dao.SMAdDao;
import spatialmarketplace.cem.model.Group;
import spatialmarketplace.cem.model.GroupMember;
import spatialmarketplace.cem.model.User;
import spatialmarketplace.cem.dao.implementation.CEMNotificationDaoImpl;
import spatialmarketplace.cem.dao.implementation.SMAdDaoImpl;
import spatialmarketplace.cem.model.CEMNotification;
import spatialmarketplace.cem.model.SMAd;

/**
 * Class that manage the business logic of Dashboard
 * @author Laura Andreina
 */
public class DashboardActionBean extends GroupActionBean {

    private static final Log logger = Log.getInstance(DashboardActionBean.class);
    private static final String VIEW = "/WEB-INF/jsp/dashboard2.jsp";
    private static final String DETAIL = "/WEB-INF/jsp/group_view.jsp";
    private static final String MEMBERLIST = "/WEB-INF/jsp/memberprofile_view.jsp";
    private static final String SENDMESSAGE = "/WEB-INF/jsp/send_message.jsp";
    private static final String WIDGET = "/WEB-INF/jsp/add_widget.jsp";
    private static final String CUSTOMIZE = "/WEB-INF/jsp/customize_dashboard.jsp";
    private static final String NOTIFICATION = "/WEB-INF/jsp/notification_view.jsp";
    private User user;
    private User memberDetails;
    String description;
    String subject;
    Date date;
    Integer groupId;
    String groupName = "";
    Integer userId;
    List<Group> myGroups = new ArrayList<Group>();
    private ArrayList<SMAd> adList;
    //For notifications
    private ArrayList<CEMNotification> notifications;
    private CEMNotification currentNotification;
    private static CEMNotificationDaoImpl notificationDao = new CEMNotificationDaoImpl();

    /*
     * The default event handler is triggered when an event handler is not
     * specified An event handler is a method that does the following: * Is
     * declared as public * Returns a Resolution * Takes no parameters * Is
     * defined in an action bean
     */
    @DefaultHandler
    public Resolution view() {
        groupName = getGroupDao().fetchGroupName(groupId);
        getContext().setGroupOrResourceId(groupId);
        getContext().setForumBelongsToGroup(true);
        loadNotifications();
        loadAdList();
        //System.out.println("+++++++++" + groupId);
        return new ForwardResolution(VIEW);
    }
    /*
     * Display the group view
     * @return new ForwardResolution()
     */

    public Resolution detail() {
        return new ForwardResolution(DETAIL);
    }
    /*
     * get the member list details
     * @return new ForwardResolution()
     */

    public Resolution memberProfile() {
        //System.out.println("--------------------->>>>>>>>" + groupId);
        try {
            memberDetails = getGroupDao().memberDetails(userId, groupId);

        } catch (Exception e) {
            ValidationError error = new SimpleError(user + " The member information cannot be displayed.");
            getContext().getValidationErrors().add("user", error);
            logger.error("Error" + e);
            System.out.println("Error: " + DashboardActionBean.class.getName() + ", " + e);
        }

        return new ForwardResolution(MEMBERLIST);
    }
    /* Display the send message view
    @return new ForwardResolution()
     */

    public Resolution send() {

        return new ForwardResolution(SENDMESSAGE);
    }
    /* Display the widget view
    @return new ForwardResolution()
     */

    public Resolution add() {

        return new ForwardResolution(WIDGET);
    }
    /* Display  customize view
    @return new ForwardResolution()
     */

    public Resolution customize() {

        return new ForwardResolution(CUSTOMIZE);
    }
    /* Display notification view
    @return new ForwardResolution()
     */

    public Resolution notification() {
        Integer notId;
        notId = Integer.valueOf(getContext().getRequest().getParameter("currentNotificationId"));
        currentNotification = getNotificationDao().getNotification(notId);
        return new ForwardResolution(NOTIFICATION);
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public Integer getGroupId() {
        return groupId;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public User getMemberDetails() {
        return memberDetails;
    }

    public void setMemberDetails(User memberDetails) {
        this.memberDetails = memberDetails;
    }

    public void setUsers(User user) {
        this.user = user;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    /* Get the group members of a group
    @return List<GroupMember>
     */
    public List<GroupMember> getGroupMembers() {
        List<GroupMember> members = new ArrayList<GroupMember>();

        try {
            members = getGroupDao().retrieveGroupMembers(groupId, getContext().getUserId());

        } catch (Exception e) {
            ValidationError error = new SimpleError(members + " The list of members cannot be displayed.");
            getContext().getValidationErrors().add("members", error);
            logger.error("Error" + e);
            System.out.println("Error: " + DashboardActionBean.class.getName() + ", " + e);
        }

        return members;

    }
    /*
     * to get the notification list of a group
     * @return new RedirectResolution()
     */

    public Resolution notifications() {
        try {
            getGroupDao().retrieveNotificationList();

        } catch (Exception e) {
            ValidationError error = new SimpleError(" The notifications cannot be displayed.");
            getContext().getValidationErrors().add("notifications", error);
            logger.error("Error" + e);
            System.out.println("Error: " + DashboardActionBean.class.getName() + ", " + e);
        }

        return new RedirectResolution(getClass());

    }

    @DontValidate
    public Resolution close() {
        return new RedirectResolution(DashboardActionBean.class);
    }

    /*
     * method to get all my groups
    @return List<Group>
     */
    public List<Group> getMyGroups() {
        Integer userId = getContext().getUserId();

        try {
            myGroups = getGroupDao().getMyGroups(userId);

        } catch (Exception e) {
            ValidationError error = new SimpleError(myGroups + " The list of group cannot be displayed.");
            getContext().getValidationErrors().add("myGroups", error);
            logger.error("Error" + e);
            System.out.println("Error: " + WelcomeScreenActionBean.class.getName() + ", " + e);
        }

        return myGroups;
    }

    protected void loadNotifications() {

        notifications = getNotificationDao().getNotifications(getContext().getUserId());

    }

    public ArrayList<CEMNotification> getNotifications() {
        return notifications;
    }

    public void setNotifications(ArrayList<CEMNotification> notifications) {
        this.notifications = notifications;
    }

    public CEMNotificationDaoImpl getNotificationDao() {
        return notificationDao;
    }

    public void setNotificationDao(CEMNotificationDaoImpl notificationD) {
        notificationDao = notificationD;
    }

    public void loadAdList() {
        SMAdDao smAdDao = SMAdDaoImpl.getInstance();
        adList = smAdDao.readAllAds();
    }

    public ArrayList<SMAd> getAdList() {
        return adList;
    }

    public CEMNotification getCurrentNotification() {
        return currentNotification;
    }

    public void setCurrentNotification(CEMNotification currentNotification) {
        this.currentNotification = currentNotification;
    }
}
